package com.cskaoyan.market.admin.service.impl;

import com.cskaoyan.market.admin.service.MarketTopicService;
import com.cskaoyan.market.db.domain.MarketTopic;
import com.cskaoyan.market.db.domain.MarketTopicExample;
import com.cskaoyan.market.db.mapper.MarketTopicMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class MarketTopicServiceImpl implements MarketTopicService{
    @Autowired
    private MarketTopicMapper topicMapper;
    
    @Override
    public List<MarketTopic> topicSelect(String title, String subtitle, String sort, String order,int limit,int page) {
        Integer currentPage = page;
        Integer pagesize = limit;
        
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        if(title==null&&subtitle==null){
            marketTopicExample.orderBy(sort + " " + order);
            marketTopicExample.or().andIdIsNotNull().andDeletedNotEqualTo(true);
            PageHelper.startPage(currentPage, pagesize);
            List<MarketTopic> marketTopics = topicMapper.selectByExample(marketTopicExample);
            
            
            return marketTopics;
        }

        if(title!=null&&subtitle==null){
            marketTopicExample.orderBy(sort + " " + order);
            marketTopicExample.or().andIdIsNotNull().andDeletedNotEqualTo(true).andTitleLike("%" + title + "%");
            PageHelper.startPage(currentPage, pagesize);
            List<MarketTopic> marketTopics = topicMapper.selectByExample(marketTopicExample);
            
            
            return marketTopics;
        }

        if(title==null&&subtitle!=null){
            marketTopicExample.orderBy(sort + " " + order);
            marketTopicExample.or().andIdIsNotNull().andDeletedNotEqualTo(true).andSubtitleLike("%" + subtitle + "%");
            PageHelper.startPage(currentPage, pagesize);
            List<MarketTopic> marketTopics = topicMapper.selectByExample(marketTopicExample);
            
            
            return marketTopics;
        }
        marketTopicExample.orderBy(sort + " " + order);
        marketTopicExample.or().andIdIsNotNull().andDeletedNotEqualTo(true).andSubtitleLike("%" + subtitle + "%").andTitleLike("%" + title + "%");
        PageHelper.startPage(currentPage, pagesize);
        List<MarketTopic> marketTopics = topicMapper.selectByExample(marketTopicExample);
        
        
        return marketTopics;
    }

    @Override
    public void deleteTopic(String id) {
        
        MarketTopic marketTopic = new MarketTopic();
        marketTopic.setDeleted(true);
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        marketTopicExample.or().andIdEqualTo(Integer.valueOf(id));

        //update deleted = 'true' from 表名 where id=#{id}
        topicMapper.updateByExampleSelective(marketTopic,marketTopicExample);
        
        
    }

    @Override
    public List<MarketTopic> topicUpdate(String id, String title, String subtitle, String content, String price, String readCount, String picUrl, String sortOrder, String goods, String deleted) {
        boolean deleted1=false;
        if(deleted.equals(true)){
            deleted1=true;
        }
        if(picUrl==null){
            picUrl="";
        }
        if(readCount==null){
            readCount="";
        }
        if(goods==null){
            goods="";
        }
        LocalDateTime now = LocalDateTime.now();
        BigDecimal price1 = new BigDecimal(price);
        
        MarketTopic marketTopic = new MarketTopic();
        marketTopic.setTitle(title);
        marketTopic.setSubtitle(subtitle);
        marketTopic.setContent(content);
        marketTopic.setPrice(price1);
        marketTopic.setReadCount(readCount);
        marketTopic.setPicUrl(picUrl);
        marketTopic.setSortOrder(Integer.valueOf(sortOrder));
        marketTopic.setDeleted(deleted1);
        marketTopic.setUpdateTime(now);

        MarketTopicExample marketTopicExample = new MarketTopicExample();
        marketTopicExample.or().andIdEqualTo(Integer.valueOf(id));
        topicMapper.updateByExampleSelective(marketTopic,marketTopicExample);
        
        MarketTopic marketTopic1 = topicMapper.selectByPrimaryKey(Integer.valueOf(id));
        List<MarketTopic> marketTopics = new ArrayList<>();
        marketTopics.add(marketTopic1);
        
        
        return marketTopics;
    }

    @Override
    public MarketTopic topicSelectById(String id) {
        
        MarketTopic marketTopic = topicMapper.selectByPrimaryKey(Integer.valueOf(id));
        return marketTopic;
    }

    @Override
    public MarketTopic topicCreate(String title, String subtitle, String content, String price, String readCount, String picUrl, Integer[] goods) {
        BigDecimal price1 = new BigDecimal(price);
        MarketTopic marketTopic = new MarketTopic();
        marketTopic.setTitle(title);
        marketTopic.setSubtitle(subtitle);
        marketTopic.setContent(content);
        marketTopic.setPrice(price1);
        marketTopic.setReadCount(readCount);
        marketTopic.setPicUrl(picUrl);
        marketTopic.setGoods(goods);
        marketTopic.setAddTime(LocalDateTime.now());
        marketTopic.setUpdateTime(LocalDateTime.now());
        marketTopic.setDeleted(false);
        
        topicMapper.insertSelective(marketTopic);
        
        
        return marketTopic;
    }
}
